package net.hancl.modules.sys.controller;

import net.hancl.common.bean.ResultBean;
import net.hancl.common.bean.page.QueryResult;
import net.hancl.modules.sys.entity.Role;
import net.hancl.modules.sys.service.IRoleService;
import net.hancl.modules.sys.utils.UserUtils;
import net.hancl.modules.sys.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "sys/role")
public class RoleController {

    @Autowired
    private UserUtils userUtils;
    @Autowired
    private IRoleService roleService;

    @RequestMapping(value = "indexRoleMark", method = RequestMethod.GET)
    @ResponseBody
    public ResultBean getIndexRoleMark(RoleVo roleVo) {
        ResultBean rb = new ResultBean();
        //当前用户拥有的角色条件 ,暂略...
//		roleVo.setUserId(userUtils.getUserBean().getId());
        QueryResult<Role> result = roleService.getRoleList(1, 20, roleVo);
        // 如果没有会话角色标识
        if (userUtils.getSession().getAttribute(UserUtils.ROLE_MARK) == null) {
            List<Role> list = result.getItems();
            if (list != null && list.size() > 0) {
                Role roleinfo = list.get(0);
                // 初始角色类型-标识
                userUtils.getSession().setAttribute(UserUtils.ROLE_MARK, roleinfo.getTypes());
            }
        }
        // 获取当前
        Object rolemark = userUtils.getSession().getAttribute(UserUtils.ROLE_MARK);
        if (rolemark != null) {
            rb.setMessage(rolemark.toString());
        }
        rb.setData(result);
        return rb;
    }
}
